'''

Case Type： 数据库系统
Case Name： 修改数据库参数max_process_memory的值，使其小于最小值
Create At:  2020/06/24
Owner    :  @li-xin12345
Descption:  1.设置max_process_memory为1024KB，小于最小值;2.设置参数时报错，设置不成功

history：
created： 2020/06/24  created by @li-xin12345
'''

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant


class PortOccupied(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case109.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.Constant = Constant()
        self.max_process_memory = 1024

    def test_max_process_memory_set_min_values(self):
        self.log.info('------------------更改参数max_process_memory=1024------------------------------')
        set_cmd = f'''
                   source {self.SOURCE_PATH};
                   gs_guc set -N all -I all -c max_process_memory={self.max_process_memory};
                 '''
        set_msg = self.primaryNode.sh(set_cmd).result()
        self.log.info(set_msg)
        res = set_msg.find(self.Constant.OUTSIDE_MSG) > -1
        self.assertTrue(res)

    def tearDown(self):
        self.log.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbClusterStatus('status')
            self.log.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()

                self.assertTrue(is_started)
                self.log.info(f'{is_started}')
                self.log.info(f'db_status: {is_started}')
        finally:
            db_status = self.sh_primy.getDbClusterStatus('status')
            self.log.info(db_status)
        self.log.info('------------------reliability_dbsys109 执行完成------------------------------')
